# -*- coding:utf-8 -*-
import random
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from base.base import Base
from handle.handle_log import log
from handle.handle_yaml import create_project_yaml_handle
from handle.handle_csv import handerCreateProjectData


class ProjectManage(Base):
    """
    项目管理
    """
    def __int__(self, driver):
        """

        :type driver: object
        """
        super().__init__(driver)
        self.driver = driver

    def create_new_project(self):
        """
        创建新项目
        :return:
        """
        ele = create_project_yaml_handle()
        data = handerCreateProjectData()
        # 进入项目管理界面
        self.driver_.get(ele["project_manage"]["manage_page"])
        # self.find_element(
        #     By.XPATH,
        #     ele["project_manage"]["manage_page"]
        # ).click()
        # self.find_element(
        #     By.XPATH,
        #     ele["project_manage"]["inter_manage"]
        # ).click()
        # 点击新建项目
        time.sleep(2)
        self.find_element(
            By.XPATH,
            ele["project_manage"]['new_project']
        ).click()
        # 选择创建新建项目
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][0]
        ).click()
        # 输入项目名称
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][1]
        ).clear()
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][1]
        ).send_keys(data['projectname'])
        # 点击选择单位
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][2]["organization"][0]
        ).click()
        # 选择建设单位
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][2]["organization"][1]
        ).click()
        # 选择项目类别为房建
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][3]["category"][0]
        ).click()
        # 选择项目地址
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][4]["project_address"][0]
        ).click()
        # 选择贵州
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][4]["project_address"][1]
        ).click()
        # 选择贵阳
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][4]["project_address"][2]
        ).click()
        # 选择观山湖区
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][4]["project_address"][3]
        ).click()
        # 输入位置
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][5]["location"][0]
        ).send_keys(data['location_x'])
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][5]["location"][1]
        ).send_keys(data['location_y'])

        # 是否开启智慧工地
        choisce_one = random.choice([0, 1]) # 随机，0表示不开启，1表示开启
        if choisce_one == 1:
            self.find_element(
                By.XPATH,
                ele["project_manage"]["create_new"][6]
            ).click()

        # 详细地址
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][7]
        ).send_keys(data['address'])
        # 完成创建
        self.find_element(
            By.XPATH,
            ele["project_manage"]["create_new"][8]
        ).click()

        # 检查验证
        text_out = ""
        try:
            text_out = self.find_element(
                By.XPATH,
                ele["project_manage"]["check_new"][0]
            ).text
        except Exception as e:
            log.error(e)
        try:
            self.find_element(
                By.XPATH,
                ele["project_manage"]["check_new"][1]
            ).send_keys(data["projectname"])
        except Exception as e:
            log.error(e)
        text = ""
        try:
            text = self.find_element(
                By.XPATH,
                ele["project_manage"]["check_new"][2]
            ).text
        except Exception as e:
            log.error(e)

        return text_out, text







